<script setup>
import { ref, onMounted } from "vue";
import {
  queryAllApi,
  addApi,
  queryByApi,
  updateApi,
  deleteApi,
} from "@/api/dept";
import { ElMessage, ElMessageBox } from "element-plus";

onMounted(() => {
  search();
});
const search = async () => {
  const result = await queryAllApi();
  if (result.code) {
    deptList.value = result.data;
  }
};
const deptList = ref([]);

const dialogFormVisible = ref(false);
const dept = ref({ name: "" });
const formTitle = ref("");

const rules = ref({
  name: [
    { required: true, message: "部门名称必须输入", trigger: "blur" },
    {
      min: 2,
      max: 10,
      message: "部门名称长度必须在2到10之间",
      trigger: "blur",
    },
  ],
});

const addDept = () => {
  dialogFormVisible.value = true;
  formTitle.value = "新增部门";
  dept.value = { name: "" };
  if (deptFormRef.value) {
    deptFormRef.value.resetFields();
  }
};
const save = async () => {
  if (!deptFormRef.value) return;
  deptFormRef.value.validate(async (valid) => {
    if (valid) {
      let result;
      if (dept.value.id) {
        result = await updateApi(dept.value);
      } else {
        result = await addApi(dept.value);
      }

      if (result.code) {
        if (!dept.value.id) {
          ElMessage.success("新增操作成功");
        } else {
          ElMessage.success("修改操作成功");
        }

        dialogFormVisible.value = false;
        search();
      } else {
        ElMessage.error(result.msg);
      }
    } else {
      ElMessage.error("表单校验不通过");
    }
  });
};

const edit = async (id) => {
  formTitle.value = "新增部门";
  if (deptFormRef.value) {
    deptFormRef.value.resetFields();
  }
  const result = await queryByApi(id);
  if (result.code) {
    dialogFormVisible.value = true;
    dept.value = result.data;
  }
};

const deptFormRef = ref();

const delDept = async (id) => {
  ElMessageBox.confirm("确认删除这个部门吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const result = await deleteApi(id);
      if (result.code) {
        ElMessage.success("删除成功");
        search();
      } else {
        ElMessage.error(result.msg);
      }
    })
    .catch(() => {
      ElMessage.info("您已取消删除");
    });
};
</script>

<template>
  <h1>新增部门</h1>

  <div class="container">
    <el-button type="primary" @click="addDept"> + 新增部门</el-button>
  </div>

  <el-dialog v-model="dialogFormVisible" :title="formTitle" width="500">
    <el-form :model="dept" :rules="rules" ref="deptFormRef">
      <el-form-item label="部门名称" label-width="80px" prop="name">
        <el-input v-model="dept.name" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>

  <div>
    <el-table :data="deptList" border style="width: 100%">
      <el-table-column type="index" width="100" label="序号" align="center" />
      <el-table-column
        prop="name"
        label="部门名称"
        width="260"
        align="center"
      />
      <el-table-column
        prop="updateTime"
        label="最后操作时间"
        width="300"
        align="center"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" @click="edit(scope.row.id)"
            ><el-icon><EditPen /></el-icon>编辑</el-button
          >
          <el-button type="danger" size="small" @click="delDept(scope.row.id)"
            ><el-icon><Delete /></el-icon>删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style scoped>
.container {
  margin: 10px 0;
}
</style>
